package unitTool;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class AlipayUtil {

    @Autowired
    private AlipayConfig alipayConfig;

    public AlipayConfig getAlipayConfig() {
        return alipayConfig;
    }

    public void setAlipayConfig(AlipayConfig alipayConfig) {
        this.alipayConfig = alipayConfig;
    }

    public String connect(AlipayBean alipayBean) throws AlipayApiException {
        System.out.println(alipayConfig);
        AlipayClient alipayClient = new DefaultAlipayClient(
                alipayConfig.getOpenApiDomain(),
                alipayConfig.getAPPID(),
                alipayConfig.getAppPrivateKey(),
                "json",
                alipayConfig.getCHARSET(),
                alipayConfig.getAlipayPublicKey(),
                alipayConfig.getRSA2()
        );
        AlipayTradePagePayRequest request =
                new AlipayTradePagePayRequest();

//        request.setNotifyUrl("http://localhost:8080/alipay/payResult");
        request.setReturnUrl("http://localhost:8080/alipay/payResult");

        System.out.println(JSON.toJSONString(alipayBean));
        request.setBizContent(JSON.toJSONString(alipayBean));
        //请求支付
        String result = alipayClient.pageExecute(request).getBody();
        return result;
    }

}
